package setting

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/setting"
	"likeadmin/core"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var PayGroup = core.Group("/setting/pay", newSettingPayHandler, regSettingPay, middleware.TokenAuth())

func newSettingPayHandler(srv setting.IPaySettingService) *settingPayHandler {
	return &settingPayHandler{srv: srv}
}

func regSettingPay(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *settingPayHandler) {
		rg.GET("/config/lists", handle.list)
		rg.GET("/config/get", handle.detail)
		rg.POST("/config/set", handle.set)
		rg.GET("/way/get", handle.getWay)
		rg.POST("/way/set", handle.setWay)
	})
}

type settingPayHandler struct {
	srv setting.IPaySettingService
}

// list setting列表
func (hd settingPayHandler) list(c *gin.Context) {
	res, err := hd.srv.List(c)
	response.CheckAndRespWithData(c, res, err)
}

// detail setting详情
func (hd settingPayHandler) detail(c *gin.Context) {
	var detailReq req.PaySettingDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(c, detailReq.Name)

	response.CheckAndRespWithData(c, res, err)
}

func (hd settingPayHandler) set(c *gin.Context) {
	var editReq req.PaySettingEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Set(c, editReq))
}

// getWay 支付场景的支付获取
func (hd settingPayHandler) getWay(c *gin.Context) {
	res, err := hd.srv.GetWay(c)
	response.CheckAndRespWithData(c, res, err)
}

// SetWay 支付场景的支付设置
func (hd settingPayHandler) setWay(c *gin.Context) {
	var setReq map[string][]req.PaySceneSetReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &setReq)) {
		return
	}
	err := hd.srv.SetWay(c, setReq)
	response.CheckAndResp(c, err)
}
